"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Col = Col;
exports.FlexMiniLayout = void 0;
exports.Row = Row;

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _components = require("@tarojs/components");

var _configContext = require("../../../extends/configContext");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var FlexContext = /*#__PURE__*/_react.default.createContext({
  gutter: 20
});

function Row(_ref) {
  var _classNames;

  var _ref$gutter = _ref.gutter,
      gutter = _ref$gutter === void 0 ? 20 : _ref$gutter,
      _ref$flexWrap = _ref.flexWrap,
      flexWrap = _ref$flexWrap === void 0 ? 'wrap' : _ref$flexWrap,
      _ref$reverse = _ref.reverse,
      reverse = _ref$reverse === void 0 ? false : _ref$reverse,
      _ref$align = _ref.align,
      align = _ref$align === void 0 ? 'stretch' : _ref$align,
      _ref$justify = _ref.justify,
      justify = _ref$justify === void 0 ? 'flex-start' : _ref$justify,
      children = _ref.children,
      className = _ref.className,
      style = _ref.style;
  var flex = null;
  var rowStyle = null;

  var _useConfig = (0, _configContext.useConfig)(),
      clsPrefix = _useConfig.clsPrefix; // 定义了 gutter 的，才生成一个 flex 上下文，否则使用默认样式即可


  if (typeof gutter === 'number') {
    var gutterCompensation = Math.ceil(Number(gutter) / 2) * -1;
    flex = {
      gutter: gutter
    };
    rowStyle = {
      margin: "".concat(gutterCompensation, "px")
    };
  }

  rowStyle = _objectSpread(_objectSpread({}, rowStyle || {}), style || {});
  var rowClassName = (0, _classnames.default)("".concat(clsPrefix, "-row"), className, (_classNames = {}, _defineProperty(_classNames, "".concat(clsPrefix, "-flex-wrap-nowrap"), flexWrap === 'nowrap'), _defineProperty(_classNames, "".concat(clsPrefix, "-align-items-").concat(align), typeof align !== 'undefined'), _defineProperty(_classNames, "".concat(clsPrefix, "-justify-content-").concat(justify), typeof justify !== 'undefined'), _defineProperty(_classNames, "".concat(clsPrefix, "-flex-row-reverse"), reverse), _classNames));
  return /*#__PURE__*/_react.default.createElement(_components.View, {
    className: rowClassName,
    style: rowStyle
  }, /*#__PURE__*/_react.default.createElement(FlexContext.Provider, {
    value: flex
  }, children));
}

function Col(_ref2) {
  var _classNames2;

  var children = _ref2.children,
      className = _ref2.className,
      _ref2$offset = _ref2.offset,
      offset = _ref2$offset === void 0 ? 0 : _ref2$offset,
      _ref2$order = _ref2.order,
      order = _ref2$order === void 0 ? 0 : _ref2$order,
      flexStyle = _ref2.flex,
      span = _ref2.span,
      style = _ref2.style;

  var _useConfig2 = (0, _configContext.useConfig)(),
      clsPrefix = _useConfig2.clsPrefix;

  var flex = (0, _react.useContext)(FlexContext);
  var colStyle = null;

  if (flex) {
    var gutterCompensation = Math.ceil(Number(flex.gutter) / 2);
    colStyle = {
      padding: "".concat(gutterCompensation, "px")
    };
  }

  colStyle = _objectSpread(_objectSpread({}, colStyle || {}), style || {});
  return /*#__PURE__*/_react.default.createElement(_components.View, {
    className: (0, _classnames.default)("".concat(clsPrefix, "-col"), className, (_classNames2 = {}, _defineProperty(_classNames2, "col-offset-".concat(offset), offset >= 1 && offset <= 11), _defineProperty(_classNames2, "col-".concat(span), span >= 1 && span <= 12), _classNames2)),
    style: _objectSpread(_objectSpread({}, colStyle), {}, {
      order: order,
      flex: flexStyle
    })
  }, children);
}

var FlexMiniLayout = {
  Row: Row,
  Col: Col
};
exports.FlexMiniLayout = FlexMiniLayout;